package com.nextwist.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.nexttwist.common.BaseException;

/**
 * This class is used for notification related method
 * @author Mintu
 *
 */
public class NotificationDAO extends BaseDAO{
	CommonDAO commonDAO = new CommonDAO();
	
	/**
	 * This method is used to get the notifications of a user
	 * @param userId
	 * @return List of notifications
	 */
	public String getNotifications(int userId) throws BaseException{
		try{
			PreparedStatement getPrepare = conn.prepareStatement("SELECT * FROM `notification` WHERE `followerid` = ? AND delflag = 'N' ORDER BY crtdtime DESC LIMIT 50");
			getPrepare.setInt(1, userId);
			ResultSet rs = getPrepare.executeQuery();
			JSONArray jsonArray = new JSONArray();
			while(rs.next()){
				int event = rs.getInt("event");
				JSONObject obj = new JSONObject();
				int uniqueId = rs.getInt("uniqueentryid");
				int storyId = rs.getInt("storyid");
				obj.put("storyId", rs.getInt("storyid"));
				obj.put("vote", rs.getInt("vote"));
				if(event == 1){
					PreparedStatement getDetailsPrepare = conn.prepareStatement("SELECT story.storyname as storyLine, entryline.entryline as entryLine from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = ? and story.storyid = ? ");
					getDetailsPrepare.setInt(1, uniqueId);
					getDetailsPrepare.setInt(2, storyId);
					ResultSet result = getDetailsPrepare.executeQuery();
					while(result.next()){
						obj.put("storyTitle", result.getString("storyLine"));
						obj.put("entryLine", result.getString("entryLine"));
					}
					int voteCount = commonDAO.getVoteUpCount(uniqueId);
					String voteText = (voteCount > 1) ? " likes" : " like";
					String notification = "The entry line has received "+voteCount+voteText;
					obj.put("notify", notification);
				} else if(event == -1){
					PreparedStatement getDetailsPrepare = conn.prepareStatement("SELECT story.storyname as storyLine, entryline.entryline as entryLine from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = ? and story.storyid = ? ");
					getDetailsPrepare.setInt(1, uniqueId);
					getDetailsPrepare.setInt(2, storyId);
					ResultSet result = getDetailsPrepare.executeQuery();
					while(result.next()){
						obj.put("storyTitle", result.getString("storyLine"));
						obj.put("entryLine", result.getString("entryLine"));
					}
					int voteCount = commonDAO.getVoteDownCount(uniqueId);
					String voteText = (voteCount > 1) ? " dislikes" : " dislike";
					String notification = "The entry line has received "+voteCount+voteText;
					obj.put("notify", notification);
				} else if(event == 2){
					PreparedStatement getDetailsPrepare = conn.prepareStatement("SELECT story.storyname as storyLine, storyline.storyline as entryLine from storyline join story on storyline.storyid = story.storyid where storyline.storylineid = 1 and story.storyid = ? and storyline.delflag = 'N'");
					getDetailsPrepare.setInt(1, storyId);
					ResultSet result = getDetailsPrepare.executeQuery();
					while(result.next()){
						obj.put("storyTitle", result.getString("storyLine"));
						obj.put("entryLine", result.getString("entryLine"));
					}
					String notification = "A new story has been posted";
					obj.put("notify", notification);
				} else if(event == 3){
					PreparedStatement getDetailsPrepare = conn.prepareStatement("SELECT story.storyname as storyLine, entryline.entryline as entryLine from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = ? and story.storyid = ? and entryline.delflag = 'N'");
					getDetailsPrepare.setInt(1, uniqueId);
					getDetailsPrepare.setInt(2, storyId);
					ResultSet result = getDetailsPrepare.executeQuery();
					while(result.next()){
						obj.put("storyTitle", result.getString("storyLine"));
						obj.put("entryLine", result.getString("entryLine"));
					}
					String notification = "A new entry line has been posted";
					obj.put("notify", notification);
				} else if(event == 4){
					PreparedStatement getDetailsPrepare = conn.prepareStatement("SELECT story.storyname as storyLine, entryline.entryline as entryLine from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = ? and story.storyid = ? and entryline.delflag = 'N'");
					getDetailsPrepare.setInt(1, uniqueId);
					getDetailsPrepare.setInt(2, storyId);
					ResultSet result = getDetailsPrepare.executeQuery();
					while(result.next()){
						obj.put("storyTitle", result.getString("storyLine"));
						obj.put("entryLine", result.getString("entryLine"));
					}
					String notification = "Congrats! Your entry line is now a story line";
					obj.put("notify", notification);
				} else if(event == 5){
					PreparedStatement getDetailsPrepare = conn.prepareStatement("SELECT story.storyname as storyLine, entryline.entryline as entryLine from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = ? and story.storyid = ? and entryline.delflag = 'N'");
					getDetailsPrepare.setInt(1, uniqueId);
					getDetailsPrepare.setInt(2, storyId);
					ResultSet result = getDetailsPrepare.executeQuery();
					while(result.next()){
						obj.put("storyTitle", result.getString("storyLine"));
						obj.put("entryLine", result.getString("entryLine"));
					}
					String notification = "The entry line has become a story line";
					obj.put("notify", notification);
				} 
				jsonArray.put(obj);
			}
			return jsonArray.toString();
		} catch (Exception e) {
			throw new BaseException(e, "500", e.getMessage());
		}
	}
}
